package com.atguigu.srb.core.service.impl;

import com.alibaba.fastjson.JSON;
import com.atguigu.srb.core.mapper.ChallengeTableMapper;
import com.atguigu.srb.core.mapper.CommunityActivitiesMapper;
import com.atguigu.srb.core.mapper.CommunityInformationMapper;
import com.atguigu.srb.core.mapper.CooperationTableMapper;
import com.atguigu.srb.core.pojo.dto.CommunityInfor;
import com.atguigu.srb.core.pojo.entity.*;
import com.atguigu.srb.core.service.CommunityInformationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.BeanUtils;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class CommunityInformationServiceImpl extends ServiceImpl<CommunityInformationMapper, CommunityInformation> implements CommunityInformationService {


    @Resource
    private RedisTemplate<String,String> redisTemplate;
    @Resource
     private CommunityInformationMapper communityInformationMapper;
    @Resource
    private ChallengeTableMapper challengeTableMapper;
    @Resource
    private CooperationTableMapper cooperationTableMapper;

    @Resource
    private CommunityActivitiesMapper communityActivitiesMapper;

    @Override
    public List<CommunityInfor> CommunityList(){

        List<CommunityInfor> communityInforList = new ArrayList<>();


        List<CommunityInformation> communityInformationList = list();


        for (CommunityInformation original :communityInformationList) {
            CommunityInfor copy = new CommunityInfor();
            BeanUtils.copyProperties(original, copy);
            System.out.println(copy);
            communityInforList.add(copy);
        }

        return communityInforList;


    }
    public Boolean redisofid(int id){

        // 获取当前登录的用户对象
        Subject subject = SecurityUtils.getSubject();
        // 从Subject对象中获取用户信息，这里假设用户信息是以User对象的形式存储的
        User currentUser = (User) subject.getPrincipal();
        //查询communityinformation对象并返回
        CommunityInformation communityInformation = communityInformationMapper.selectById(id);

        HashOperations<String, String, String> hashOperations = redisTemplate.opsForHash();

        // 存储到 Redis 的哈希结构中
        // 第一个参数是哈希结构的键（key），这里使用当前用户的用户名
        // 第二个参数是哈希结构中的字段名（field），这里使用社区编号
       // 第三个参数是与字段名相关联的值（value），这里是社区信息的 JSON 字符串
        hashOperations.put(currentUser.getUsername(), communityInformation.getCommunityNumber(), JSON.toJSONString(communityInformation));
        return true;

    }

    @Override
    public List<ChallengeTable> getchallengtable(String communitynumber) {

        List<ChallengeTable> challengeTables =challengeTableMapper.selectByRespondingClubNumber02(communitynumber);


        return challengeTables;
    }

    @Override
    public List<CooperationTable> getcooperation(String communitynumber) {
        List<CooperationTable> challengeTables =cooperationTableMapper.selectByRespondingClubNumber02(communitynumber);


        return challengeTables;
    }

    @Override
    public List<CommunityActivities> selectacticity() {

        List<CommunityActivities> communityActivities = communityActivitiesMapper.selectAll();
        return communityActivities;
    }


}

